package com.darwin.darwinGpuEngine.core.materials {

    import com.darwin.darwinGpuEngine.utils.TextureHelper;
    
    import flash.display.BitmapData;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    public class SpriteSheet extends ASpriteSheetBase {

        private var nullOffset:Point = new Point();
		public var numSheets:uint = 0;
        /**
         *
         * @param sheetWidth
         * @param sheetHeight
         * @param spriteWidth
         * @param spriteHeight
         * @param fps
         * @param spritesPackedWithoutSpace 
         */
        public function SpriteSheet(sheetWidth:Number, sheetHeight:Number, spriteWidth:Number, spriteHeight:Number, fps:uint,
                                    spritesPackedWithoutSpace:Boolean = false) {
            this.fps = fps;
            this.spritesPackedWithoutSpace = spritesPackedWithoutSpace;

            _spriteWidth = spriteWidth;
            _spriteHeight = spriteHeight;
            _sheetWidth = sheetWidth;
            _sheetHeight = sheetHeight;

            generateSheet();
        }

        private function generateSheet():void {
            var numSheetsPerRow:int = Math.round(_sheetWidth / spriteWidth);
            var numRows:int = Math.round(_sheetHeight / spriteHeight);
			numSheets = numSheetsPerRow * numRows;
            var rowIdx:uint;
            var colIdx:uint;

            uvRects = new Vector.<Rectangle>(numSheets, true);
            frames = new Vector.<Rectangle>();
            uvRects = new Vector.<Rectangle>(numSheets, true);

            for(var i:int = 0; i < numSheets; i++) {
                rowIdx = i % numSheetsPerRow;
                colIdx = Math.floor(i / numSheetsPerRow);

                frames.push(new Rectangle((spriteWidth * rowIdx),
                                          (spriteHeight * colIdx),
                                           _spriteWidth,
                                           _spriteHeight));
            }
        }

		public function get frameRate():uint
		{
			return fps;
		}
		
		public function set frameRate(value:uint):void
		{
			fps = value;
		}
		
        override public function getOffsetForFrame():Point {
            return nullOffset;
        }

        override public function clone():ASpriteSheetBase {

            var s:SpriteSheet = new SpriteSheet(_sheetWidth, _sheetHeight, _spriteWidth, _spriteHeight, fps, spritesPackedWithoutSpace);

            for(var name:String in animationMap) {
                var anim:SpriteSheetAnimation = animationMap[name];
                s.addAnimation(name, anim.frames.concat(), anim.loop, false);
            }

            s.frame = frame;

            return s;
        }
    }
}
